package com.dk.domain;

import com.alibaba.excel.annotation.ExcelProperty;
import com.dk.annotation.EnumValue;
import com.dk.domain.assertion.CheckPointEntity;
import com.dk.util.log.Log;
import lombok.Data;
import okhttp3.Response;
import okhttp3.internal.http2.Header;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * 接口自动化测试实体类
 */
@Data
public class ApiAutoTestEntity {
    @ExcelProperty("CaseNum(用例编号)")
    @NotNull(message = "CaseNum(用例编号)不能为空")
    @Size(max = 15,message = "CaseNum(用例编号)不得超过15位")
    private String caseNum;

    @ExcelProperty("ApiNum(接口编号)")
    @NotNull(message = "ApiNum(接口编号)不能为空")
    @Size(max = 15,message = "ApiNum(接口编号)不得超过15位")
    private String apiNum;

    @ExcelProperty("ApiName(接口名)")
    @NotNull(message = "ApiName(接口名)不能为空")
    @Size(max = 50,message = "ApiName(接口名)不得超过50位")
    private String apiName;


    @ExcelProperty("CaseName(用例名)")
    @NotNull(message = "CaseName(用例名)不能为空")
    @Size(max = 50,message = "CaseName(用例名)不得超过50位")
    private String caseName;

    @ExcelProperty("Protocol(请求协议)")
    @NotNull(message = "Protocol(请求协议)不能为空")
    @EnumValue(strValues = {"HTTP","HTTPS"},message = "Protocol(请求协议)不正确(枚举值:HTTP,HTTPS)")
    private String protocol;

    @ExcelProperty("Method(请求方式)")
    @NotNull(message = "Method(请求方式)不能为空")
    @EnumValue(strValues = {"GET","POST","DELETE","PUT"},message = "Method(请求方式)不正确(枚举值:GET,POST,DELETE,PUT)")
    private String method;

    @ExcelProperty("url(请求地址)")
    @NotNull(message = "url(请求地址)不能为空")
    @Size(max = 1000,message = "url(请求地址)不得超过1000位")
    private String url;

    @ExcelProperty("Headers(请求头部)")
    private String headers;

    @ExcelProperty("BodyType(请求类型)")
    @NotNull(message = "BodyType(请求类型)不能为空")
    @EnumValue(strValues = {"JSON","PARAMS","XML","FILE"},message = "BodyType(请求类型)不正确(枚举值:JSON,PARAMS,XML,FILE)")
    private String bodyType;

    @ExcelProperty("Body(请求数据)")
    private String body;

    @ExcelProperty("BeforeAction(前置动作)")
    private String beforeAction;

    @ExcelProperty("CheckPoint(检查点)")
    @NotNull(message = "CheckPoint(检查点)不能为空")
    private String checkPoint;

    @ExcelProperty("AfterAction(后置动作)")
    private String afterAction;

    //响应主体
    //private Response response;
    private String resHeaders;
    private String resCode;
    private String resBody;
    //断言结果
    private Boolean assertResult;

    /**
     * 重写equals方法,防止用例编码重复
     * @param o
     * @return
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ApiAutoTestEntity apiAutoTestEntity = (ApiAutoTestEntity) o;
        //只是比较apiNum,只有属性值一致才会返回true
        Log.error("---加载数据中发现CaseNum(用例编号)存在重复项,已剔除后续重复项,请检查数据,重复用例编码:"+caseNum+";");
        return caseNum != null ? caseNum.equals(apiAutoTestEntity.caseNum) : apiAutoTestEntity.caseNum == null;
    }

    /**
     * 重写hashCode方法，返回的hashCode不一样才会再去比较每一个属性的值
     * @return
     */
    @Override
    public int hashCode() {
        //return apiNum.hashCode
        return caseNum != null ? caseNum.hashCode() : 0;
    }
}
